<template>
  <div
    class="avue-contail"
    :class="{'avue--collapse':isCollapse}"
  >
    <!-- <screenshot></screenshot> -->
    <div class="avue-header">
      <!-- 顶部导航栏 -->
      <top ref="top" />
    </div>

    <div class="avue-layout">
      <div class="avue-left">
        <!-- 左侧导航栏 -->
        <sidebar />
      </div>
      <div
        class="avue-main"
        :class="{'avue-main--fullscreen':!isMenu}"
      >
        <!-- 顶部标签卡 -->
        <tags />
        <transition name="fade-scale">
          <search
            v-show="isSearch"
            class="avue-view"
          />
        </transition>
        <!-- 主体视图层 -->
        <div
          v-show="!isSearch"
          id="avue-view"
          style="height:100%;overflow-y:auto;overflow-x:hidden;"
        >
          <keep-alive>
            <router-view
              v-if="$route.meta.keepAlive"
              class="avue-view"
            />
          </keep-alive>
          <router-view
            v-if="!$route.meta.keepAlive"
            class="avue-view"
          />
        </div>
      </div>
    </div>
    <div
      class="avue-shade"
      @click="showCollapse"
    />

    <preview :visible.sync="previewVisible" :program-data="programData" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import tags from './tags'
// import screenshot from './screenshot'
import search from './search'
import top from './top/'
import sidebar from './sidebar/'
import admin from '@/util/admin'
import { validatenull } from '@/util/validate'
import { calcDate } from '@/util/date.js'
import { getStore } from '@/util/store.js'
import Preview from '@/views/ips/editor/preview'
export default {
  name: 'Index',
  components: {
    top,
    tags,
    search,
    sidebar,
    // screenshot,
    Preview
  },
  provide() {
    return {
      index: this
    }
  },
  data() {
    return {
      // 搜索控制
      isSearch: false,
      // 刷新token锁
      refreshLock: false,
      // 刷新token的时间
      refreshTime: '',
      previewVisible: false,
      programData: null
    }
  },
  computed: mapGetters(['isMenu', 'isLock', 'isCollapse', 'website', 'menu']),
  created() {
    // 实时检测刷新token
    // this.refreshToken();
  },
  mounted() {
    this.init()
    this.$EventBus.$on('PREVIEW', data => {
      this.$nextTick(() => {
        console.log('PREVIEW', data)
        this.programData = data
        this.previewVisible = true
      })
    })
    this.$once('PREVIEW', () => {
      // this.$EventBus.$off(EVENT_INCRE)
    })
  },
  methods: {
    showCollapse() {
      this.$store.commit('SET_COLLAPSE')
    },
    // 屏幕检测
    init() {
      this.$store.commit('SET_SCREEN', admin.getScreen())
      window.onresize = () => {
        setTimeout(() => {
          this.$store.commit('SET_SCREEN', admin.getScreen())
        }, 0)
      }
    },
    // 打开菜单
    openMenu(item = {}) {
      this.$store.dispatch('GetMenu', item.parentId).then(data => {
        // console.log('data', data)
        if (data.length !== 0) {
          this.$router.$avueRouter.formatRoutes(data, true)
        }
        // 当点击顶部菜单做的事件
        if (!this.validatenull(item)) {
          let itemActive = {}
          const childItemActive = 0
          // vue-router路由
          if (item.path) {
            itemActive = item
          } else {
            if (this.menu[childItemActive].length === 0) {
              itemActive = this.menu[childItemActive]
            } else {
              itemActive = this.menu[childItemActive].children[childItemActive]
            }
          }
          this.$store.commit('SET_MENUID', item)
          this.$router.push({
            path: this.$router.$avueRouter.getPath({
              name: itemActive.label,
              src: itemActive.path
            }, itemActive.meta)
          })
        }
      })
    },
    // 10分钟检测一次token
    refreshToken() {
      this.refreshTime = setInterval(() => {
        const token = getStore({
          name: 'token',
          debug: true
        }) || {}
        const date = calcDate(token.datetime, new Date().getTime())
        if (validatenull(date)) return
        if (date.seconds >= this.website.tokenTime && !this.refreshLock) {
          this.refreshLock = true
          this.$store
            .dispatch('RefeshToken')
            .then(() => {
              this.refreshLock = false
            })
            .catch(() => {
              this.refreshLock = false
            })
        }
      }, 1000)
    }
  }
}
</script>
